package com.gongpingjialoan.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.sell.SellPrivateCarDetailActivity;
import com.gongpingjialoan.utility.DhUtil;

/**
 * Created by Administrator on 2016/9/9.
 */
public class ModifySellCarMessageDialog extends Dialog implements View.OnClickListener {

    private int padding;

    private TextView titleTextView, messageTextView, wanTextView;

    private ImageView mImageView;

    private Button button;

    private EditText mEditText;

    private SellPrivateCarDetailActivity mActivity;

    private String type;

    public ModifySellCarMessageDialog(Context context, String type) {
        super(context, R.style.dialogs);
        mActivity = (SellPrivateCarDetailActivity) context;
        padding = DhUtil.dip2px(context, 20);
        this.type = type;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_message_layout);
        Window win = getWindow();
        win.getDecorView().setPadding(padding, 0, padding, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        mImageView = (ImageView) findViewById(R.id.cancle);
        titleTextView = (TextView) findViewById(R.id.title);
        messageTextView = (TextView) findViewById(R.id.message);
        wanTextView = (TextView) findViewById(R.id.wan);
        mEditText = (EditText) findViewById(R.id.edit);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        mImageView.setOnClickListener(this);
        if ("phone".equals(type)) {
            titleTextView.setText("修改手机号码");
            messageTextView.setText("手机号码");
            mEditText.setInputType(InputType.TYPE_CLASS_PHONE);
            mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        } else if ("contact".equals(type)) {
            titleTextView.setText("修改联系人");
            messageTextView.setText("联系人");
            mEditText.setInputType(InputType.TYPE_CLASS_TEXT);
            mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)});
        } else if ("price".equals(type)) {
            titleTextView.setText("修改价格");
            messageTextView.setText("价格");
            wanTextView.setVisibility(View.VISIBLE);
            mEditText.setInputType(InputType.TYPE_CLASS_PHONE);
            mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
        }
    }

    @Override
    public void onClick(View v) {
        if (v == button) {
            String value = mEditText.getText().toString();
            if ("phone".equals(type)) {
                if (TextUtils.isEmpty(value) || value.length() < 11) {
                    Toast.makeText(getContext(), "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
            } else if ("contact".equals(type)) {
                if (TextUtils.isEmpty(value)) {
                    Toast.makeText(getContext(), "请输入联系人", Toast.LENGTH_SHORT).show();
                    return;
                }
            } else if ("price".equals(type)) {
                if (TextUtils.isEmpty(value)) {
                    Toast.makeText(getContext(), "请输入价格", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            if(mActivity.mOffSellPrivateCarDetailFragment.isAdded()){
                mActivity.mOffSellPrivateCarDetailFragment.modifyMessage(type, value);
            }

            dismiss();
        }else if (v == mImageView) {
            dismiss();
        }
    }
}
